﻿using Microsoft.Win32;
using movie.tool.library.utils;
using movie.tool.module.film.model;
using movie.tool.module.film.service;
using movie.tool.module.http;
using System.IO;
using System.Net.Http;
using System.Windows;
using System.Windows.Controls;

namespace movie.tool.module.film.ui
{
    /// <summary>
    /// MovieCoverControl.xaml 的交互逻辑
    /// </summary>
    public partial class MovieCoverControl : UserControl
    {
        public static readonly DependencyProperty ConfigProperty = DependencyProperty.Register("Config", typeof(MovieProject), typeof(MovieCoverControl));
        public MovieProject Config
        {
            get => (MovieProject)GetValue(ConfigProperty);
            set => SetValue(ConfigProperty, value);
        }

        public static readonly DependencyProperty BusyInfoProperty = DependencyProperty.Register("BusyInfo", typeof(BusyInfo), typeof(MovieCoverControl));
        public BusyInfo BusyInfo
        {
            get => (BusyInfo)GetValue(BusyInfoProperty);
            set => SetValue(BusyInfoProperty, value);
        }

        public MovieCoverControl()
        {
            InitializeComponent();
        }

        private async void CoverBrowserClick(object sender, RoutedEventArgs e)
        {
            if (BusyInfo.Busy)
            {
                return;
            }
            try
            {
                BusyInfo.Busy = true;
                BusyInfo.DefaultMessage();
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = "图片文件(*.png,*.jpg,*.jpeg,*.bmp)|*.png;*.jpg;*.jpeg;*.bmp";
                dialog.Title = "选择封面";
                if (!string.IsNullOrEmpty(Config.Movie.Cover))
                {
                    string filePath = Path.Join(MovieService.ProjectJsonFilePath(Config.Id), Config.Movie.Cover);
                    dialog.InitialDirectory = Path.GetDirectoryName(filePath);
                }

                bool? result = dialog.ShowDialog();
                if (result != null && result == true)
                {
                    string selectedFile = dialog.FileName;
                    string rootPath = MovieService.ProjectJsonFilePath(Config.Id);
                    string path = FileUtils.Copy(selectedFile, rootPath, "cover");
                    string storePath = Path.GetRelativePath(rootPath, path);
                    if (Config.ImportProject)
                    {
                        Config.Movie.Cover = storePath;
                    }
                    else
                    {
                        bool res = await MovieClient.UpdateMovieCover(selectedFile, storePath, Config.Id);
                        if (res)
                        {
                            MessageBox.Show("保存成功");
                            Config.Movie.Cover = storePath;
                            MovieService.SaveProjectMovie(Config);
                        }
                    }
                }
            }
            finally
            {
                BusyInfo.Reset();
            }
        }

        private async void CoverDownloadClick(object sender, RoutedEventArgs e)
        {
            if (BusyInfo.Busy)
            {
                return;
            }
            try
            {
                BusyInfo.Busy = true;
                BusyInfo.BusyMessage = "下载中...";
                MovieSynchronizer synchronizer = new MovieSynchronizer(BusyInfo, Config.Id);
                await synchronizer.SyncCoverFile(Config);
            }
            finally
            {
                BusyInfo.Reset();
            }
        }

        private async void AssistBrowserClick(object sender, RoutedEventArgs e)
        {
            if (BusyInfo.Busy)
            {
                return;
            }
            try
            {
                BusyInfo.Busy = true;
                BusyInfo.DefaultMessage();
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = "word文档(*.doc,*.docx,*.wps)|*.doc;*.docx;*.wps";
                dialog.Title = "选择剧本手册";
                if (!string.IsNullOrEmpty(Config.Movie.Assist))
                {
                    string filePath = Path.Join(MovieService.ProjectJsonFilePath(Config.Id), Config.Movie.Assist);
                    dialog.InitialDirectory = Path.GetDirectoryName(filePath);
                }

                bool? result = dialog.ShowDialog();
                if (result != null && result == true)
                {
                    string selectedFile = dialog.FileName;
                    string rootPath = MovieService.ProjectJsonFilePath(Config.Id);
                    string path = FileUtils.Copy(selectedFile, rootPath, "assist");
                    string storePath = Path.GetRelativePath(rootPath, path);
                    if (Config.ImportProject)
                    {
                        Config.Movie.Assist = storePath;
                    }
                    else
                    {
                        bool res = await MovieClient.UpdateMovieAssist(selectedFile, storePath, Config.Id);
                        if (res)
                        {
                            MessageBox.Show("保存成功");
                            Config.Movie.Assist = storePath;
                            MovieService.SaveProjectMovie(Config);
                        }
                    }
                }
            }
            finally
            {
                BusyInfo.Reset();
            }
        }

        private async void AssistDownloadClick(object sender, RoutedEventArgs e)
        {
            if (BusyInfo.Busy)
            {
                return;
            }
            try
            {
                BusyInfo.Busy = true;
                BusyInfo.BusyMessage = "下载中...";
                MovieSynchronizer synchronizer = new MovieSynchronizer(BusyInfo, Config.Id);
                await synchronizer.SyncAssistFile(Config);
            }
            finally
            {
                BusyInfo.Reset();
            }
        }

        private async void ClearAssistClick(object sender, RoutedEventArgs e)
        {
            if (BusyInfo.Busy)
            {
                return;
            }
            try
            {
                BusyInfo.Busy = true;
                BusyInfo.BusyMessage = "处理中...";
                MovieSynchronizer synchronizer = new MovieSynchronizer(BusyInfo, Config.Id);
                if(await synchronizer.ClearAssistFile())
                {
                    Config.Movie.Assist = "";
                    MovieService.SaveProjectMovie(Config);
                }
            }
            finally
            {
                BusyInfo.Reset();
            }
        }
    }
}
